package com.moody.util;

import java.lang.reflect.Field;
import java.util.List;

import com.moody.hibernate.domain.ReportData;
import com.moody.hibernate.domain.ResultDisplayForFindCustomerInFinancialAnalysis;

/*
 * Copyright ? Moody analytics   
 *
 * @Title: CopyUtil.java 
 * @Package： com.moody.util 
 * @author： Liu Peng
 * @Description: TODO(用一句话描述该文件做什么)    
 * @date： 2012-5-24 上午10:12:42
 * @Modified by: Liu Peng 
 * @version V1.0   
 */

public class CopyUtil {
	
	public static boolean copy(Object origin, Object destination){
		Class co = origin.getClass();
		Class cd = destination.getClass();
		Field[] coField = co.getDeclaredFields();
		Field[] cdField = cd.getDeclaredFields();
		for(int i = 0; i < coField.length; i++){
			String coFieldName = coField[i].getName();
			for(int j = 0; j < cdField.length; j++){
				if(cdField[j].getName().equals(coFieldName)){
					try {
						coField[i].setAccessible(true);
						cdField[j].setAccessible(true);
						cdField[j].set(destination, "" + coField[i].get(origin));
						
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return false;
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ArrayIndexOutOfBoundsException e){
						System.out.println(coFieldName);
					}
				}
			}
		}
		return true;
	}
	
	public static boolean copyFinancialIndex(List<ReportData> list, ResultDisplayForFindCustomerInFinancialAnalysis displayItem){
		if(list.size() == 0) return false;
		Class<ResultDisplayForFindCustomerInFinancialAnalysis> des = ResultDisplayForFindCustomerInFinancialAnalysis.class;
		//为了显示需要，在这里我们将SFR存储到FR中
		boolean isSmall = false;
		ReportData temp = list.get(0);
		if(temp.getReportItemNo().substring(0, 3).equals("SFR"))isSmall = true;
		
		for(ReportData rd :list){
			try {
				String itemNo = rd.getReportItemNo();
				if(isSmall) itemNo = itemNo.substring(1,itemNo.length());
				
				des.getDeclaredField(itemNo).setAccessible(true);
				des.getDeclaredField(itemNo).set(displayItem, "" + rd.getItemValue());
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;
	}

}
